//
//  TTOrderViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/10/8.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTOrderViewCell.h"
#import "TTOrderGoodsViewCell.h"
#import "TTLuoshiViewCell.h"
#import "TTOrderModel.h"
#import "TTOrderDetailViewController.h"
#import <UIButton+WebCache.h>

static NSString *cellID = @"goods_cell_id";
static NSString *luoshiCellID = @"luoshi_cell_id";

@interface TTOrderViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<TTOrderShopModel *> *dataArray;
@property (nonatomic, strong) NSDictionary *orderStatusDic; // 订单状态

@end

@implementation TTOrderViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.orderStatusDic = @{
            @"10" : @"待确认",
            @"20" : @"处理中",
            @"60" : @"待发货",
            @"70" : @"待结算",
        };
        
        
    }
    return self;
}

- (void)setModel:(TTOrderModel *)model
{
    _model = model;
    self.dataArray = model.Detail;
    
    self.bgView.frame = CGRectMake(5, 5, SCREEN_WIDTH - 10, model.H - 10);
    self.tableView.frame = self.bgView.bounds;
    
//    self.bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds cornerRadius:5].CGPath;
    
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTOrderShopModel *model = self.dataArray[indexPath.row];
    if (model.GoodsType == 1) {
        // 裸石
        TTLuoshiViewCell *cell = [tableView dequeueReusableCellWithIdentifier:luoshiCellID];
        [cell setModel:model hasShadow:YES isGray:NO];
//        cell.backgroundColor = UIColorHex(#F5F5F5);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        TTOrderGoodsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        [cell setModel:model hasShadow:YES isGray:NO];
//        cell.backgroundColor = UIColorHex(#F5F5F5);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTOrderDetailViewController *detailVC = [[TTOrderDetailViewController alloc] initWithOrderNo:self.model.OrderNo];
    [[LLRouteTool getCurrentNavigationController] pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTOrderShopModel *model = self.dataArray[indexPath.row];
    return model.h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (void)lookDetailBtnClick
{
    TTOrderDetailViewController *detailVC = [[TTOrderDetailViewController alloc] initWithOrderNo:self.model.OrderNo];
    [[LLRouteTool getCurrentNavigationController] pushViewController:detailVC animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width, 40)];
    
    UIStackView *stView = [[UIStackView alloc] initWithFrame:CGRectMake(10, 0, view.width - 20, 40)];
    stView.axis = UILayoutConstraintAxisHorizontal;
    stView.distribution = UIStackViewDistributionFill;
    stView.alignment = UIStackViewAlignmentCenter;
    [view addSubview:stView];
    
    NSString *str = [NSString stringWithFormat:@"订单号:%@", self.model.OrderNo];
    UILabel *orderNumberLabel = [UILabel labelWithTitle:str color:UIColorHex(333333) fontSize:13];
    
//    NSString *statusStr = [TTOrderStatusUtilis getStatusTitleByOrderStatus:self.model.OrderState];
    UIButton *btn = [UIButton createButtonWithTitle:self.model.OrderStateName titleColor:kMainColor titleFont:[UIFont systemFontOfSize:15] backgroundColor:nil target:self action:@selector(lookDetailBtnClick)];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"order_right_arrow"]];
    
    [stView addArrangedSubview:orderNumberLabel];
    [stView addArrangedSubview:btn];
    [stView addArrangedSubview:imageView];
    
//    [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.height) endPoint:CGPointMake(view.width - 10, view.height) lineColor:kSeparatorColor];
    
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.model.OrderRecord.IsShow ? 94 : 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CGFloat h = self.model.OrderRecord.IsShow ? 94 : 50;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width, h)];
    
    NSString *price = [NSString stringWithFormat:@"￥%@", self.model.OrderRealPayPrice];
    NSString *oneStr = [NSString stringWithFormat:@"共%ld件    合计:%@", self.model.Detail.count, price];
    UILabel *oneLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:16];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:oneStr];
    [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(FF1F1F) range:[oneStr rangeOfString:price]];
    oneLabel.attributedText = attr;
    
    UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneBtn setTitle:@"联系导购" forState:UIControlStateNormal];
    [oneBtn setTitleColor:UIColorHex(666666) forState:UIControlStateNormal];
    oneBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    oneBtn.layer.borderColor = UIColorHex(666666).CGColor;
    oneBtn.layer.borderWidth = 1;
    oneBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 8, 3, 8);
    [oneBtn addTarget:self action:@selector(clickOneButton) forControlEvents:UIControlEventTouchUpInside];
    
    NSInteger orderState = self.model.OrderState.integerValue;
    NSString *title = [TTOrderStatusUtilis getTitleByOrderStatus:orderState];
    
    UIButton *twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoBtn setTitle:title forState:UIControlStateNormal];
    [twoBtn setTitleColor:UIColorHex(666666) forState:UIControlStateNormal];
    twoBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    twoBtn.layer.borderColor = UIColorHex(666666).CGColor;
    twoBtn.layer.borderWidth = 1;
    twoBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 8, 3, 8);
    [twoBtn addTarget:self action:@selector(clickTwoButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIStackView *stView = [[UIStackView alloc] initWithFrame:CGRectMake(10, 0, view.width - 20, 50)];
    stView.axis = UILayoutConstraintAxisHorizontal;
    stView.distribution = UIStackViewDistributionFill;
    stView.alignment = UIStackViewAlignmentCenter;
    stView.spacing = 5;
    [view addSubview:stView];
    
    [stView addArrangedSubview:oneLabel];
    [stView addArrangedSubview:oneBtn];
    [stView addArrangedSubview:twoBtn];
    
    [stView layoutIfNeeded];
    oneBtn.layer.cornerRadius = oneBtn.height * 0.5;
    twoBtn.layer.cornerRadius = twoBtn.height * 0.5;
    
    UIView *btnBGView = [[UIView alloc] initWithFrame:CGRectMake(10, stView.bottom, view.width - 20, 30)];
    btnBGView.backgroundColor = UIColorHex(#F4F4F4);
    btnBGView.layer.cornerRadius = 5;
    [view addSubview:btnBGView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 20, btnBGView.height)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.model.OrderRecord.Icon] placeholderImage:nil options:SDWebImageRetryFailed];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [btnBGView addSubview:imgView];
    
    NSString *btnTitle = self.model.OrderRecord.Record;
    UIButton *btn = [UIButton createButtonWithTitle:btnTitle titleColor:[UIColor colorWithHexString:self.model.OrderRecord.Color] titleFont:[UIFont systemFontOfSize:12] backgroundColor:nil target:nil action:nil];
    btn.enabled = NO;
    btn.frame = CGRectMake(imgView.right + 10, 0, view.width - 20, 30);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btnBGView addSubview:btn];
    
    btnBGView.hidden = !self.model.OrderRecord.IsShow;
    
    @weakify(self)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        TTOrderDetailViewController *detailVC = [[TTOrderDetailViewController alloc] initWithOrderNo:self.model.OrderNo];
        [[LLRouteTool getCurrentNavigationController] pushViewController:detailVC animated:YES];
    }];
    [view addGestureRecognizer:tap];
    return view;
}

// 点击第一个按钮: 联系导购
- (void)clickOneButton
{
    // 打电话
    [[TTLoginSingle sharedSingle].loginModel.SalesmanMobile callPhone];
}

// 点击第二个按钮
- (void)clickTwoButton:(UIButton *)sender
{
    NSString *title = sender.titleLabel.text;
    if ([title isEqualToString:orderCancelString]) {
        if ([self.delegate respondsToSelector:@selector(cancleButtonClickWithOrderViewCell:model:)]) {
            [self.delegate cancleButtonClickWithOrderViewCell:self model:self.model];
        }
    } else if ([title isEqualToString:orderProgressString]) {
        if ([self.delegate respondsToSelector:@selector(checkScheduleButtonClickWithOrderViewCell:model:)]) {
            [self.delegate checkScheduleButtonClickWithOrderViewCell:self model:self.model];
        }
    } else if ([title isEqualToString:orderDeleteString]) {
        if ([self.delegate respondsToSelector:@selector(deleteButtonClickWithOrderViewCell:model:)]) {
            [self.delegate deleteButtonClickWithOrderViewCell:self model:self.model];
        }
    }
    
}


- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        [self.contentView addSubview:_bgView];
        // 阴影
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 10;
//        _bgView.layer.shadowColor = kMainColor.CGColor;
//        _bgView.layer.shadowOffset = CGSizeZero;
//        _bgView.layer.shadowOpacity = 0.2;
        
    }
    return _bgView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[TTOrderGoodsViewCell class] forCellReuseIdentifier:cellID];
        [_tableView registerClass:[TTLuoshiViewCell class] forCellReuseIdentifier:luoshiCellID];
        [self.bgView addSubview:_tableView];
    }
    return _tableView;
}



@end
